/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.visual.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.common.constant.LauncherConstant;
import org.springblade.common.utils.RequestUtil;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.tool.jackson.JsonUtil;
import org.springblade.core.tool.support.Kv;
import org.springblade.core.tool.utils.CollectionUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.StringPool;
import org.springblade.core.tool.utils.StringUtil;
import org.springblade.modules.visual.entity.VisualProxy;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Map;

/**
 * 接口代理 控制器
 *
 * @author Chill
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping(LauncherConstant.APPLICATION_VISUAL_NAME + "/visual")
@ApiIgnore
@Api(value = "接口代理", tags = "接口代理")
public class VisualProxyController extends BladeController {

	/**
	 * 接口代理
	 */
	@RequestMapping("/proxy")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "接口代理", notes = "接口代理")
	public Object proxy(@RequestBody VisualProxy proxy) {
		// 获取参数
		String url = proxy.getUrl();
		String method = proxy.getMethod();
		Map<String, Object> headers = proxy.getHeaders();
		Map<String, Object> params = proxy.getParams();
		Object data = proxy.getData();
		String request;
		// 获取参数提交格式
		Boolean form = Func.toBoolean(proxy.getHeaders().get("form"), true);
		// 处理GET与DELETE方法请求
		if (StringUtil.equalsIgnoreCase(method, RequestUtil.GET) || StringUtil.equalsIgnoreCase(method, RequestUtil.DELETE)) {
			request = RequestUtil.request(url, headers, params, method);
		} else {
			// 处理POST与PUT方法请求
			if (form) {
				request = RequestUtil.requestForm(url + StringPool.QUESTION_MARK + StringUtil.trimAllWhitespace(String.valueOf(data)), headers, params, method);
			} else {
				request = RequestUtil.requestJson(url, headers, JsonUtil.toJson(data), method);
			}
		}
		// 判断返回结果集
		if (StringUtil.isBlank(request)) {
			return Kv.create().set("code", 400).set("msg", "获取数据失败,请检查参数配置!");
		} else {
			Map<String, Object> result = JsonUtil.toMap(request);
			return CollectionUtil.isEmpty(result) ? request : result;
		}
	}

}
